package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.FinanceModel;
import one.stand.model.ResultModel;
import one.stand.request.FinanceRequest;
import one.stand.response.FinanceResponse;
import one.stand.service.BaseService;
import one.stand.service.FinanceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@Service
public class FinanceServiceImpl extends BaseService implements FinanceService {

    @Override
    public ResultModel getGrossIncome(FinanceRequest request) {
        log.info("公司财务统计-总收入:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        FinanceModel model = new FinanceModel();
        if (request.getRoleId().intValue() > 0) {
            model.setCompanyId(request.getCId().intValue());
        } else {
            model.setCardId(request.getMyCardId().intValue());
        }
        // 总成交金额
        Integer money = inputDealMoneyMapper.selectFinanceSum(model);
        // 退款金额
        Integer refundMoney = orderinfoMapper.selectFinanceRefund(model);
        return ResultModel.success((money == null ? 0 : money) - (refundMoney == null ? 0 : refundMoney));
    }

    @Override
    public ResultModel<Map<String, Object>> selectFinanceMall(FinanceRequest request) {
        log.info("公司财务统计-商城流水:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        FinanceModel model = new FinanceModel();
        BeanUtils.copyProperties(request, model);
        model.setCardId(request.getMyCardId());
        model.setCompanyId(request.getCId().intValue());
        List<FinanceModel> list = orderinfoMapper.selectFinanceMall(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<FinanceResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                FinanceResponse response = new FinanceResponse();
                BeanUtils.copyProperties(obj, response);
                response.setUserName(StringUtils.isBlank(obj.getUserName())?"":new String(java.util.Base64.getDecoder().decode(obj.getUserName())));
                responseList.add(response);
            }
        });
        PageInfo<FinanceResponse> pageInfo = new PageInfo<>(responseList);
        Integer money = orderinfoMapper.selectFinanceMallMoney(model);
        Map<String, Object> map = new HashMap<>(2);
        map.put("money", money);
        map.put("data", JSONArray.parse(JSON.toJSONString(pageInfo.getList())));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> selectFinanceDeal(FinanceRequest request) {
        log.info("公司财务统计-成交流水:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        FinanceModel model = new FinanceModel();
        BeanUtils.copyProperties(request, model);
        model.setUserId(request.getId().intValue());
        model.setCardId(request.getMyCardId());
        model.setCompanyId(request.getCId().intValue());
        List<FinanceModel> list = inputDealMoneyMapper.selectFinanceDeal(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<FinanceResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                FinanceResponse response = new FinanceResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        PageInfo<FinanceResponse> pageInfo = new PageInfo<>(responseList);
        Integer money = inputDealMoneyMapper.selectFinanceDealMoney(model);
        Map<String, Object> map = new HashMap<>(2);
        map.put("money", money);
        map.put("data", JSONArray.parse(JSON.toJSONString(pageInfo.getList())));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> selectFinanceCompany(FinanceRequest request) {
        log.info("公司财务统计-公司流水:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        FinanceModel model = new FinanceModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId().intValue());
        List<FinanceModel> list = inputDealMoneyMapper.selectFinanceDeal(model);
        if (list == null || list.size() <= 0) {
            return ResultModel.noData();
        }
        List<FinanceResponse> responseList = new ArrayList<>();
        list.forEach(obj -> {
            {
                FinanceResponse response = new FinanceResponse();
                BeanUtils.copyProperties(obj, response);
                responseList.add(response);
            }
        });
        PageInfo<FinanceResponse> pageInfo = new PageInfo<>(responseList);
        Integer money = inputDealMoneyMapper.selectFinanceDealMoney(model);
        Map<String, Object> map = new HashMap<>(2);
        map.put("money", money);
        map.put("data", JSONArray.parse(JSON.toJSONString(pageInfo.getList())));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> selectCustomerNum(FinanceRequest request) {
        log.info("公司销售排行-客户人数:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        FinanceModel model = new FinanceModel();
        BeanUtils.copyProperties(request, model);
        model.setCompanyId(request.getCId().intValue());
        //PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<FinanceModel> financeModelList = customerMapper.selectUserSort(model);
        if (financeModelList == null || financeModelList.size() <= 0) {
            return ResultModel.noData();
        }
        List<FinanceResponse> responseList = new ArrayList<>();
        FinanceModel myModel = new FinanceModel();
        for(FinanceModel obj : financeModelList){
            FinanceResponse response = new FinanceResponse();
            BeanUtils.copyProperties(obj, response);
            responseList.add(response);
            if(request.getMyCardId().equals(obj.getCardId())){
                myModel = obj;
            }
        }
        //PageInfo<FinanceResponse> pageInfo = new PageInfo<>(responseList);
        Map<String, Object> map = new HashMap<>(2);
        map.put("my", JSONArray.parse(JSON.toJSONString(myModel)));
        map.put("data", JSONArray.parse(JSON.toJSONString(responseList)));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> selectOrderNum(FinanceRequest request) {
        log.info("公司销售排行-订单量:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        FinanceModel model = new FinanceModel();
        BeanUtils.copyProperties(request, model);
        model.setCardId(request.getMyCardId());
        model.setCompanyId(request.getCId().intValue());
        List<FinanceModel> financeModelList = new ArrayList<>();
        //PageHelper.startPage(request.getPageNum(), request.getPageSize());
        // 订单总数
        if (request.getType() == 1) {
            financeModelList = orderinfoMapper.selectOrderNumSort(model);
            // 成交金额
        } else if (request.getType() == 2) {
            financeModelList = inputDealMoneyMapper.selectInputDealSort(model);
            // 月目标
        } else if (request.getType() == 3) {
            financeModelList = userTaskMapper.selectUserTaskSort(model);
        }
        if (financeModelList == null || financeModelList.size() <= 0) {
            return ResultModel.noData();
        }
        List<FinanceResponse> responseList = new ArrayList<>();
        FinanceResponse financeResponse = new FinanceResponse();
        for (FinanceModel obj : financeModelList){
            FinanceResponse response = new FinanceResponse();
            BeanUtils.copyProperties(obj, response);
            responseList.add(response);
            if(request.getMyCardId().equals(obj.getCardId())){
                financeResponse=response;
            }
        }
        //PageInfo<FinanceResponse> pageInfo = new PageInfo<>(responseList);
        Map<String, Object> map = new HashMap<>(2);
        map.put("my", JSONArray.parse(JSON.toJSONString(financeResponse)));
        map.put("data", JSONArray.parse(JSON.toJSONString(responseList)));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> selectInteractionNum(FinanceRequest request) {
        log.info("公司销售排行-互动频率:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        FinanceModel model = new FinanceModel();
        BeanUtils.copyProperties(request, model);
        model.setCardId(request.getMyCardId());
        model.setCompanyId(request.getCId().intValue());
        List<FinanceModel> financeModelList = new ArrayList<>();
        //PageHelper.startPage(request.getPageNum(), request.getPageSize());
        // 跟进总数
        if (request.getType() == 1) {
            financeModelList = followRecordMapper.selectFollowSort(model);
            // 咨询总数
        } else if (request.getType() == 2) {
            financeModelList = followRecordMapper.selectConsultationSort(model);
        }
        if (financeModelList == null || financeModelList.size() <= 0) {
            return ResultModel.noData();
        }
        List<FinanceResponse> responseList = new ArrayList<>();
        FinanceModel myModel = new FinanceModel();
        for(FinanceModel obj : financeModelList){
            FinanceResponse response = new FinanceResponse();
            BeanUtils.copyProperties(obj, response);
            responseList.add(response);
            if(request.getMyCardId().equals(obj.getCardId())){
                myModel = obj;
            }
        }
        //PageInfo<FinanceResponse> pageInfo = new PageInfo<>(responseList);
        Map<String, Object> map = new HashMap<>(2);
        map.put("my", JSONArray.parse(JSON.toJSONString(myModel)));
        map.put("data", JSONArray.parse(JSON.toJSONString(responseList)));
        return ResultModel.success(map);
    }

    @Override
    public ResultModel<Map<String, Object>> selectDelNum(FinanceRequest request) {
        log.info("公司销售排行-成交率区间:{}", JSON.toJSONString(request));
        if (request == null) {
            return ResultModel.checkParamFail();
        }
        FinanceModel model = new FinanceModel();
        BeanUtils.copyProperties(request, model);
        model.setCardId(request.getMyCardId());
        model.setCompanyId(request.getCId().intValue());
        //PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<FinanceModel> financeModelList = orderinfoMapper.selectTurnoverRateSort(model);
        if (financeModelList == null || financeModelList.size() <= 0) {
            return ResultModel.noData();
        }
        List<FinanceResponse> responseList = new ArrayList<>();
        FinanceModel myModel = new FinanceModel();
        for(FinanceModel obj : financeModelList){
            FinanceResponse response = new FinanceResponse();
            BeanUtils.copyProperties(obj, response);
            responseList.add(response);
            if(request.getMyCardId().equals(obj.getCardId())){
                myModel = obj;
            }
        }
        //PageInfo<FinanceResponse> pageInfo = new PageInfo<>(responseList);
        Map<String, Object> map = new HashMap<>(2);
        map.put("my", JSONArray.parse(JSON.toJSONString(myModel)));
        map.put("data", JSONArray.parse(JSON.toJSONString(responseList)));
        return ResultModel.success(map);
    }
}
